package me.yangbajing.web

import net.liftweb.http.{LiftRules, RewriteResponse, ParsePath, RewriteRequest}

object MeURI {
  val rewrite: LiftRules.RewritePF = {
    case rq@RewriteRequest(ParsePath("doc" :: subs, _, _, _), _, _) =>
      _rewriteDocument(subs, rq)

    case rq@RewriteRequest(ParsePath("image" :: subs, _, _, _), _, _) =>
      _rewriteImage(subs, rq)

    case rq@RewriteRequest(ParsePath("category" :: subs, _, _, _), _, _) =>
      _rewriteCategory(subs, rq)

    case rq@RewriteRequest(ParsePath("tag" :: subs, _, _, _), _, _) =>
      _rewriteTag(subs, rq)

    case rq@RewriteRequest(ParsePath("user" :: subs, _, _, _), _, _) =>
      _rewriteUser(subs, rq)

    case rq@RewriteRequest(ParsePath(page :: Nil, _, _, _), _, _) =>
      val params = for (h <- rq.httpRequest.params if h.values.nonEmpty) yield h.name -> h.values.head
      RewriteResponse(List("h", page), params.toMap)
  }

  private def _rewriteUser(subs: List[String], rq: RewriteRequest): RewriteResponse = {
    var path = List.empty[String]
    var params = Map.empty[String, String]

    subs match {
      case Nil =>
        path = List("h", "user", "index")

      case userId :: Nil =>
        path = List("h", "user", "detail")
        params = Map("user_id" -> userId)

      case _ =>
        path = List("h", "404")
    }

    RewriteResponse(path, params)
  }

  private def _rewriteImage(subs: List[String], rq: RewriteRequest): RewriteResponse = {
    var path = List.empty[String]
    var params = Map.empty[String, String]

    subs match {
      case Nil =>
        path = List("h", "image", "index")

      case imageId :: Nil =>
        path = List("h", "image", "detail")
        params = Map("image_id" -> imageId)

      case _ =>
        path = List("h", "404")
    }

    RewriteResponse(path, params)
  }

  private def _rewriteTag(subs: List[String], rq: RewriteRequest): RewriteResponse = {
    var path = List.empty[String]
    var params = Map.empty[String, String]

    subs match {
      case Nil =>
        path = List("h", "tag", "index")

      case tagId :: Nil =>
        path = List("h", "document", "index")
        params = Map("tag_id" -> tagId)

      case _ =>
        path = List("h", "404")
    }

    RewriteResponse(path, params)
  }

  private def _rewriteCategory(subs: List[String], rq: RewriteRequest): RewriteResponse = {
    var path = List.empty[String]
    var params = Map.empty[String, String]

    subs match {
      case Nil =>
        path = List("h", "category", "index")

      case categoryId :: Nil =>
        path = List("h", "document", "index")
        params = Map("category_id" -> categoryId)

      case _ =>
        path = List("h", "404")
    }

    RewriteResponse(path, params)
  }

  private def _rewriteDocument(subs: List[String], rq: RewriteRequest): RewriteResponse = {
    var path = List.empty[String]
    var params = Map.empty[String, String]

    subs match {
      case Nil =>
        path = List("h", "document", "index")

      case "new" :: Nil =>
        path = List("h", "document", "edit")

      case docId :: Nil =>
        path = List("h", "document", "show")
        params = Map("document_id" -> docId)

      case docId :: edit :: Nil =>
        path = List("h", "document", "edit")
        params = Map("document_id" -> docId)

      case _ =>
        path = List("h", "404")
    }

    RewriteResponse(path, params)
  }

}
